package com.project.controller.web;

import com.project.entity.SchoolManagerInfo;
import com.project.entity.User;
import com.project.myBeans.ResponseBean;
import com.project.myBeans.SchoolManagerDetail;
import com.project.service.SchoolManagerInfoService;
import com.project.service.SchoolMangerService;
import com.project.service.UserService;
import com.project.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * author: WheelChen
 * date: 2017/7/27
 * <p>
 * 学院管理员相关操作
 */
@Controller
@RequestMapping("/schoolMangerInfo")
public class SchoolManagerInfoController {
    @Autowired
    private SchoolManagerInfoService schoolManagerInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private SchoolMangerService schoolMangerService;


    @RequestMapping(value = "/findByUserId/{userId}", method = RequestMethod.GET)
    @ResponseBody
    /** Todo 如何做成事务
     * author: WheelChen
     * date: 2017/7/27
     * function: 根据userId查找学院管理员信息
     *
     * @param userId
     * @return
     */
    public ResponseBean findByUserId(@PathVariable("userId") Integer userId) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(schoolManagerInfoService.findByUserId(userId));
            responseBean.setSuccess(true);
            responseBean.setMessage("获取成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/addAccount", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/8
     * function: 创建学院管理员账号
     *
     * @param schoolManagerDetail  user表 + schoolManagerInfo表
     * @return
     */
    public ResponseBean addAccount(@RequestBody SchoolManagerDetail schoolManagerDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            User user = schoolManagerDetail.getUser();
            SchoolManagerInfo schoolManagerInfo = schoolManagerDetail.getSchoolManagerInfo();
            if (user != null) {
                if (userService.checkUsernameIsExist(user.getUsername())) {
                    responseBean.setSuccess(false);
                    responseBean.setMessage("用户名已存在");
                } else {
                    //设置用户类型
                    user.setUsertype(Constants.USERTYPE.SCHOOL.value());
                    //创建后的用户自动启用 不需要审核
                    user.setState(Constants.USERSTATE.PASS.value());
                    if (schoolManagerInfo != null) {
                        schoolManagerDetail.setUser(user);
                        schoolMangerService.addAccount(schoolManagerDetail);
                    } else {
                        responseBean.setSuccess(false);
                        responseBean.setMessage("学院管理员信息不存在");
                    }
                }
            } else {
                responseBean.setSuccess(false);
                responseBean.setMessage("用户基本信息不存在");
            }
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

}
